package com.supcon.exchangedata.service;

import com.supcon.exchangedata.pojo.entity.oracle.CtKpiDay;
import com.supcon.exchangedata.pojo.entity.oracle.CtKpiHour;
import com.supcon.exchangedata.repository.master.MasterKpiDayRepository;
import com.supcon.exchangedata.repository.master.MasterKpiHourRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Created with IntelliJ IDEA.
 * 手工插入数据
 *
 * @Author: yangbin
 * @Date: 2022/06/08/8:57
 * @Description:
 */
@Service
public class KpiDataInsertService {
    @Resource
    private MasterKpiDayRepository kpiDayRepository;
    @Resource
    private MasterKpiHourRepository kpiHourRepository;

    public void insertKpiDay(String code, String unit) {
        for (int i = 0; i < 20; i++) {
            LocalDateTime localDateTime = LocalDate.now().minusDays(i).atStartOfDay();
            CtKpiDay byCodeAndDayOfMonthKey = kpiDayRepository.findByCodeAndDayOfMonthKey(code, localDateTime);
            if (byCodeAndDayOfMonthKey == null) {
                CtKpiDay ctKpiDay = new CtKpiDay();
                ctKpiDay.setDataType(code);
                ctKpiDay.setCode(code);
                ctKpiDay.setDayOfMonthKey(localDateTime);
                ctKpiDay.setCurrentValue("0");
                ctKpiDay.setLastUpdateTime(LocalDateTime.now());
                ctKpiDay.setUnit(unit);
                kpiDayRepository.save(ctKpiDay);
            }
        }


    }

    public void insertKpiHour(String code, String unit) {

        LocalDateTime localDateTime = LocalDate.now().minusDays(1).atStartOfDay();
        for (int i = 0; i < 24; i++) {
            CtKpiHour byCodeAndCollectDateAndCollectHour = kpiHourRepository.findByCodeAndCollectDateAndCollectHour(code, localDateTime, i);
            if (byCodeAndCollectDateAndCollectHour == null) {
                CtKpiHour ctKpiHour = new CtKpiHour();
                ctKpiHour.setCode(code);
                ctKpiHour.setCurrentValue("0");
                ctKpiHour.setCollectDate(localDateTime);
                ctKpiHour.setCollectHour(i);
                ctKpiHour.setDataType(code);
                ctKpiHour.setUnit(unit);
                kpiHourRepository.save(ctKpiHour);
            }
        }




    }
}
